/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.deploy.rest;

import com.alibaba.fastjson.JSON;
import com.alien.admin.annotation.Log;
import com.alien.admin.modules.deploy.domain.DpServerConfig;
import com.alien.admin.modules.deploy.domain.ServiceDeployParam;
import com.alien.admin.modules.deploy.domain.ServiceDeployParamDto;
import com.alien.admin.modules.deploy.service.DpServerConfigService;
import com.alien.admin.modules.deploy.service.dto.DpServerConfigDto;
import com.alien.admin.modules.deploy.service.dto.DpServerConfigQueryCriteria;
import com.alien.admin.modules.utils.YamlCicdTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author zengzhifang
* @date 2021-09-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "项目构建与部署配置值管理")
@RequestMapping("/api/dpServerConfig")
@Slf4j
public class DpServerConfigController {

    private final DpServerConfigService dpServerConfigService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dpServerConfig:list')")
    public void download(HttpServletResponse response, DpServerConfigQueryCriteria criteria) throws IOException {
        dpServerConfigService.download(dpServerConfigService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询项目构建与部署配置值")
    @ApiOperation("查询项目构建与部署配置值")
    @PreAuthorize("@el.check('dpServerConfig:list')")
    public ResponseEntity<Object> query(DpServerConfigQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(dpServerConfigService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增项目构建与部署配置值")
    @ApiOperation("新增项目构建与部署配置值")
    @PreAuthorize("@el.check('dpServerConfig:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody DpServerConfig resources){
        return new ResponseEntity<>(dpServerConfigService.create(resources),HttpStatus.CREATED);
    }

    @GetMapping(value = "getByProjectIdAndEnv")
    @Log("查询项目构建与部署配置值")
    @ApiOperation("查询项目构建与部署配置值")
    @PreAuthorize("@el.check('dpServerConfig:list')")
    public ResponseEntity<Object> getByProjectIdAndEnv(DpServerConfig resources){
        DpServerConfigDto dto = dpServerConfigService.findFirstByProjectIdAndEnv(resources.getProjectId(), resources.getEnv());
        return new ResponseEntity<>(dto,HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改项目构建与部署配置值")
    @ApiOperation("修改项目构建与部署配置值")
    @PreAuthorize("@el.check('dpServerConfig:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody DpServerConfig resources){
        dpServerConfigService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping(value = "saveOrUpdate")
    @Log("修改项目构建与部署配置值")
    @ApiOperation("修改项目构建与部署配置值")
    @PreAuthorize("@el.check('dpServerConfig:edit')")
    public ResponseEntity<Object> saveOrUpdate(@Validated @RequestBody DpServerConfig resources){
        dpServerConfigService.saveOrUpdate(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping(value = "saveOrUpdate2")
    @Log("修改项目构建与部署配置值")
    @ApiOperation("修改项目构建与部署配置值")
    @PreAuthorize("@el.check('dpServerConfig:edit')")
    public ResponseEntity<Object> saveOrUpdate2(@Validated @RequestBody ServiceDeployParamDto dto){
        log.info(JSON.toJSONString(dto));
        ServiceDeployParam po = new ServiceDeployParam();
        BeanUtils.copyProperties(dto, po);
        String deployParamJson = JSON.toJSONString(po);

        DpServerConfig resources =new DpServerConfig();
        resources.setProjectId(dto.getProjectId());
        resources.setEnv(dto.getEnv());
        resources.setDeployParam(deployParamJson);
        resources.setModifyDeployParam(true);
        dpServerConfigService.saveOrUpdate(resources);

        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除项目构建与部署配置值")
    @ApiOperation("删除项目构建与部署配置值")
    @PreAuthorize("@el.check('dpServerConfig:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        dpServerConfigService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
